<template>
  <div class="rank" ref="rank">
    <scroll :data="topList" class="toplist" ref="toplist">
      <ul>
        <li class="item" v-for="item in topList" :key="item.id" @click="selectItem(item)">
          <div class="icon">
            <img width="100" height="100" v-lazy="item.coverImgUrl" alt="">
          </div>
          <ul class="songlist">
            <li class="song" v-for="(song, index) in item.tracks" :key="index">
              <span>{{ index + 1 }}</span>
              <span>{{ song.first }} - {{ song.second }}</span>
            </li>
          </ul>
        </li>
      </ul>
      <Loading class="loading-container" v-show="!topList.length"></Loading>
    </scroll>
    <router-view></router-view>
  </div>
</template>

<script>
import { getTopList } from '../../api/index.js'
import Scroll from '../../base/scroll/Scroll'
import Loading from '../../base/loading/Loading'
import { playlistMixin } from '../../assets/js/mixin'
import { mapMutations } from 'vuex'

export default {
  name: 'Rank',
  mixins: [playlistMixin],
  created() {
    this._getTopList()
  },
  data() {
    return {
      topList: []
    }
  },
  methods: {
    handlePlaylist(playlist) {
      const bottom = playlist.length ? '60px' : ''
      this.$refs.rank.style.bottom = bottom
      this.$refs.toplist.refresh()
    },
    selectItem(item) {
      this.$router.push({
        path: `/rank/${item.id}`
      })
      this.setTopList(item)
    },
    _getTopList() {
      getTopList().then(res => {
        if (res.statusText === 'OK') {
          this.topList = res.data.list
          console.log(this.topList)
        }
      })
    },
    ...mapMutations({
      setTopList: 'SET_TOP_LIST'
    })
  },
  components: {
    Scroll,
    Loading
  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/css/variable.scss";
@import "../../assets/css/mixin.scss";
.rank{
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;
  .toplist{
    height: 100%;
    overflow: hidden;
    .item{
      display: flex;
      margin: 0 20px;
      padding-top: 20px;
      height: 100px;
      &:last-child{
        padding-bottom: 20px;
      }
      .icon{
        flex: 0 0 100px;
        width: 100px;
        height: 100px;
      }
      .songlist{
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 0 20px;
        height: 100px;
        overflow: hidden;
        background: $color-highlight-background;
        color: $color-text-d;
        font-size: $font-size-small;
      }
      .song{
        @include no-wrap();
        line-height: 26px;
      }
    }
    .loading-container{
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
  }
}
</style>